package cn.iocoder.yudao.module.digital.controller.admin.worksaudio;

import org.springframework.web.bind.annotation.*;
import jakarta.annotation.Resource;
import org.springframework.validation.annotation.Validated;
import org.springframework.security.access.prepost.PreAuthorize;
import io.swagger.v3.oas.annotations.tags.Tag;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.Operation;

import jakarta.validation.constraints.*;
import jakarta.validation.*;
import jakarta.servlet.http.*;
import java.util.*;
import java.io.IOException;

import cn.iocoder.yudao.framework.common.pojo.PageParam;
import cn.iocoder.yudao.framework.common.pojo.PageResult;
import cn.iocoder.yudao.framework.common.pojo.CommonResult;
import cn.iocoder.yudao.framework.common.util.object.BeanUtils;
import static cn.iocoder.yudao.framework.common.pojo.CommonResult.success;

import cn.iocoder.yudao.framework.excel.core.util.ExcelUtils;

import cn.iocoder.yudao.framework.apilog.core.annotation.ApiAccessLog;
import static cn.iocoder.yudao.framework.apilog.core.enums.OperateTypeEnum.*;

import cn.iocoder.yudao.module.digital.controller.admin.worksaudio.vo.*;
import cn.iocoder.yudao.module.digital.dal.dataobject.worksaudio.WorksAudioDO;
import cn.iocoder.yudao.module.digital.service.worksaudio.WorksAudioService;

@Tag(name = "管理后台 - 数字人声音作品")
@RestController
@RequestMapping("/digital/works-audio")
@Validated
public class WorksAudioController {

    @Resource
    private WorksAudioService worksAudioService;

    @PostMapping("/create")
    @Operation(summary = "创建数字人声音作品")
    @PreAuthorize("@ss.hasPermission('digital:works-audio:create')")
    public CommonResult<Long> createWorksAudio(@Valid @RequestBody WorksAudioSaveReqVO createReqVO) {
        return success(worksAudioService.createWorksAudio(createReqVO));
    }

    @PutMapping("/update")
    @Operation(summary = "更新数字人声音作品")
    @PreAuthorize("@ss.hasPermission('digital:works-audio:update')")
    public CommonResult<Boolean> updateWorksAudio(@Valid @RequestBody WorksAudioSaveReqVO updateReqVO) {
        worksAudioService.updateWorksAudio(updateReqVO);
        return success(true);
    }

    @DeleteMapping("/delete")
    @Operation(summary = "删除数字人声音作品")
    @Parameter(name = "id", description = "编号", required = true)
    @PreAuthorize("@ss.hasPermission('digital:works-audio:delete')")
    public CommonResult<Boolean> deleteWorksAudio(@RequestParam("id") Long id) {
        worksAudioService.deleteWorksAudio(id);
        return success(true);
    }

    @GetMapping("/get")
    @Operation(summary = "获得数字人声音作品")
    @Parameter(name = "id", description = "编号", required = true, example = "1024")
    @PreAuthorize("@ss.hasPermission('digital:works-audio:query')")
    public CommonResult<WorksAudioRespVO> getWorksAudio(@RequestParam("id") Long id) {
        WorksAudioDO worksAudio = worksAudioService.getWorksAudio(id);
        return success(BeanUtils.toBean(worksAudio, WorksAudioRespVO.class));
    }

    @GetMapping("/page")
    @Operation(summary = "获得数字人声音作品分页")
    @PreAuthorize("@ss.hasPermission('digital:works-audio:query')")
    public CommonResult<PageResult<WorksAudioRespVO>> getWorksAudioPage(@Valid WorksAudioPageReqVO pageReqVO) {
        PageResult<WorksAudioDO> pageResult = worksAudioService.getWorksAudioPage(pageReqVO);
        return success(BeanUtils.toBean(pageResult, WorksAudioRespVO.class));
    }

    @GetMapping("/export-excel")
    @Operation(summary = "导出数字人声音作品 Excel")
    @PreAuthorize("@ss.hasPermission('digital:works-audio:export')")
    @ApiAccessLog(operateType = EXPORT)
    public void exportWorksAudioExcel(@Valid WorksAudioPageReqVO pageReqVO,
              HttpServletResponse response) throws IOException {
        pageReqVO.setPageSize(PageParam.PAGE_SIZE_NONE);
        List<WorksAudioDO> list = worksAudioService.getWorksAudioPage(pageReqVO).getList();
        // 导出 Excel
        ExcelUtils.write(response, "数字人声音作品.xls", "数据", WorksAudioRespVO.class,
                        BeanUtils.toBean(list, WorksAudioRespVO.class));
    }

}